﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.IO;

namespace PeisongWeb.admin
{
    public partial class productList : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
                if (hasPermission("BC", 1))
                {
                    drpBind();
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }
        private void drpBind()
        {

            drp_Type.DataSource = logic.category.select("treelevel=1", "sort", 200, 1).Tables[1];// logic.category.list();
            drp_Type.DataBind();
            drp_Type.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择类别--", ""));
        }
        void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("1=1");
            if (txt_proNo.Text.Trim() != "")
                where.AppendFormat("and productNo ='{0}'", txt_proNo.Text.Trim());
            if (txt_proName.Text.Trim() != "")
                where.AppendFormat("and name like '%{0}%'", txt_proName.Text.Trim());
            if (drp_Type.SelectedValue.Trim() != "")
                where.AppendFormat("and categoryId = {0}", drp_Type.SelectedValue.Trim());
            DataSet ds = logic.product.select(where.ToString(), "productId desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }
        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                int productId = Convert.ToInt32(e.CommandArgument);
                string infor;
                logic.product.delete(productId);
                rptBind();
                infor = "删除成功！";
                Library.Script.ClientMsg(infor);
            }
            else if (e.CommandName=="CopyInsert")
            {
                int productId = Convert.ToInt32(e.CommandArgument);
                hdCurrentProductId.Value = e.CommandArgument.ToString();
                txtRename.Text = logic.product.getById(productId).Name;
                windows1.Show();
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            rptBind();
        }

        protected void btnSave_Click(object sender,EventArgs e)
        {
            if (Request.Headers["Accept"] != "*/*")
            {
                int productId = 0;
                int.TryParse(hdCurrentProductId.Value, out productId);
                if (productId != 0)
                {
                    DateTime now = DateTime.Now;
                    // 新创建的文件名
                    string fileName = string.Format("{2}{1}{0}{3}{4}{5}{6}", new object[] { now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, now.Millisecond });

                    DataTable dt = logic.files.getListByProduct(productId);
                    string ext = string.Empty;

                    if (dt.Rows.Count > 0)
                    {
                        string oldFileName = dt.Rows[0]["systemName"].ToString();
                        ext = Path.GetExtension(oldFileName);

                        string fileOriginal = Server.MapPath(Library.Resource.UploadPath + Library.Resource.FileOriginal) + "\\" + oldFileName;
                        string fileThumnail1 = Server.MapPath(Library.Resource.UploadPath + Library.Resource.FileThumbnail1) + "\\" + oldFileName;
                        string fileThumnail2 = Server.MapPath(Library.Resource.UploadPath + Library.Resource.FileThumbnail2) + "\\" + oldFileName;

                        // 复制图片文件
                        if (File.Exists(fileOriginal))
                        {
                            File.Copy(fileOriginal, (Server.MapPath(Library.Resource.UploadPath + Library.Resource.FileOriginal) + "\\" + fileName + ext));
                        }
                        if (File.Exists(fileThumnail1))
                        {
                            File.Copy(fileThumnail1, (Server.MapPath(Library.Resource.UploadPath + Library.Resource.FileThumbnail1) + "\\" + fileName + ext));
                        }
                        if (File.Exists(fileThumnail2))
                        {
                            File.Copy(fileThumnail2, (Server.MapPath(Library.Resource.UploadPath + Library.Resource.FileThumbnail2) + "\\" + fileName + ext));
                        }

                    }

                    string rename = txtRename.Text;// 新商品名称 

                    logic.product.copyInsert(productId, rename, (fileName + ext));

                    Library.Script.ClientMsg("复制录入成功.");

                    rptBind();
                }
            }
        }
    }
}
